const app = getApp()

Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    picker: ['定静楼一', '定静楼二'],
    index: 0,
    classbuilddata: '',
    classnodata: '',
    items: [{
        name: '0',
        value: '显示屏黑屏/异常/无法启动'
      },
      {
        name: '1',
        value: '主机无法启动'
      },
      {
        name: '2',
        value: '投影异常/模糊/倒置/无法投影'
      },
      {
        name: '3',
        value: '声音/音响问题'
      },
      {
        name: '4',
        value: '有线麦克风故障'
      },
      {
        name: '5',
        value: '无线麦克风故障'
      },
      {
        name: '6',
        value: '鼠标、键盘故障'
      },
      {
        name: '7',
        value: '网络连接异常'
      },

      {
        name: '8',
        value: 'U盘无法读取'
      },
      {
        name: '9',
        value: '其他'
      }
    ],
    classroomChange: ['A201',
      'A202',
      'A301',
      'A302',
      'A400',
      'A401',
      'A402',
      'A500',
      'A501',
      'A502',
      'A601',
      'A602',
      'B201',
      'B202',
      'B301',
      'B302',
      'B401',
      'B402',
      'B501',
      'B502',
      'B601',
      'B602',
      'C201',
      'C202',
      'C203',
      'C204',
      'C205',
      'C301',
      'C302',
      'C303',
      'C304',
      'C305',
      'C401',
      'C402',
      'C403',
      'C404',
      'C405',
      'C501',
      'C502',
      'C503',
      'C504',
      'C505',
      'C601',
      'C602',
      'C603',
      'C604',
      'C605',
      'D201',
      'D202',
      'D301',
      'D302',
      'D401',
      'D402',
      'D501',
      'D502',
      'D601',
      'D602',
      'E201',
      'E202',
      'E301',
      'E302',
      'E401',
      'E402',
      'E501',
      'E502',
      'E601',
      'E602',
      'F201',
      'F202',
      'F301',
      'F302',
      'F401',
      'F402',
      'F501',
      'F502',
      'F601',
      'F602',



    ],
    classroomChange2: ['A201',
      'A202',
      'A203',
      'A204',
      'A205',
      'A206',
      'A207',
      'A301',
      'A302',
      'A303',
      'A304',
      'A305',
      'A306',
      'A307',
      'A401',
      'A402',
      'A403',
      'A404',
      'A405',
      'A406',
      'A407',
      'A501',
      'A502',
      'A503',
      'A504',
      'A505',
      'A506',
      'A507',
      'A601',
      'A602',
      'A603',
      'A604',
      'A605',
      'A606',
      'A607',
      'B201',
      'B202',
      'B203',
      'B204',
      'B301',
      'B302',
      'B303',
      'B304',
      'B401',
      'B402',
      'B403',
      'B404',
      'B501',
      'B502',
      'B503',
      'B504',
      'B601',
      'B602',
      'B603',
      'B604',
      'C201',
      'C202',
      'C301',
      'C302',
      'C401',
      'C402',
      'C501',
      'C502',
      'C601',
      'C602',
      'D201',
      'D202',
      'D203',
      'D301',
      'D302',
      'D401',
      'D402',
      'D403',
      'D501',
      'D502',
      'D601',
      'D602'
    ],
    time: [{
        name: '0',
        value: '马上处理'
      },
      {
        name: '1',
        value: '课间处理'
      },


    ],
    list: [{
      name: 'slide-top',
      color: 'green'
    }],

    toggleDelay: false,
    check1: true,
    check2: true,
    check3: true,
    check4: true,
    check5: true,
    // modaltext:'请稍后'
    isShowPlaceholder: '输入课室号',
    isShowPlaceholder1: '请输入问题描述   ' 
  },
  
  bindFocus: function(e) {
    
    this.setData({
      isShowPlaceholder: ''
    })
   
  },
  bindBlur: function() {
    this.setData({
      isShowPlaceholder: '输入课室号'
    })
    
  },
  bindFocus1: function(e) {
    this.setData({
      isShowPlaceholder1: ''
    })
   
  },
  bindBlur1: function() {
    this.setData({
      isShowPlaceholder1: '请输入问题描述'
    })
    
  },
  toggle: function(e) {
    // console.log(this)
    let checkdata = this.data
    if (checkdata.check1 && checkdata.check2 && checkdata.check3 && checkdata.check4 && checkdata.check5 && checkdata.check6) {
      wx.requestSubscribeMessage({
        tmplIds: ['TW8hmC2fYIWVV48pHkZ1vbbf8fQ4TSmBnjWHOfT1oT8'],
        success(res) {
          console.log('弹窗调用成功')
        },
        fail(res) {
          console.log(res)
        }
      })
    }
    // console.log(e);
    var anmiaton = e.currentTarget.dataset.class;
    var that = this;
    that.setData({
      animation: anmiaton
    })
    setTimeout(function() {
      that.setData({
        animation: ''
      })
    }, 1000)
    that.showModal()
  },
  toggleDelay() {
    var that = this;
    that.setData({
      toggleDelay: true
    })
    setTimeout(function() {
      that.setData({
        toggleDelay: false
      })
    }, 1000)
  },
  showModal: function(getdata) {

    if (this.data.classbuild == undefined) {
      this.setData({
        modalName: 'DialogModal1',
        modaltext: '教学楼数据格式有误，请重新输入。',

      })
    } else if (this.data.check2 == false) {
      this.setData({

        modalName: 'DialogModal1',
        modaltext: '课室号数据有误，请重新输入。',

      })
    } else if (this.data.check6 == false) {
      this.setData({
        modalName: 'DialogModal1',
        modaltext: '没有该课室，请重新输入。',

      })

    } else if (this.data.question == undefined) {
      this.setData({
        modalName: 'DialogModal1',
        modaltext: '问题陈述数据有误，请重新输入。',

      })
    } else if (this.data.check4 == false) {

      this.setData({
        modalName: 'DialogModal1',
        modaltext: '选择其他请填写下方问题陈述。',
      })

    } else if (this.data.time_lim == undefined) {
      this.setData({
        modalName: 'DialogModal1',
        modaltext: '时限要求数据有误，请重新输入。',

      })
    } else if (getdata == false) {
      this.setData({
        modalName: 'DialogModal1',
        modaltext: '请求太频繁，请稍后再试。'
      })
    } else if (getdata == 2) {
      this.setData({
        modalName: 'DialogModal1',
        modaltext: '网络故障。'
      })
    } else if (getdata == true) {
      this.setData({
        modalName: 'DialogModal1',
        modaltext: "报障成功，等待处理，请稍后"
      })
    }
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },




  radioChange: function(e) {
    var that = this

    console.log('radio发生change事件，携带value值为：', e.detail.value)
    that.setData({
      question: e.detail.value
    })
  },
  radioChange2: function(e) {
    var that = this
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    that.setData({
      time_lim: e.detail.value
    })
  },
  btn_fun: function(e) {

    var that = this


    that.setData({
      check1: true,
      check2: true,
      check3: true,
      check4: true,
      check5: true,
      check6: true
    })

    // console.log((that.data.classbuild == 0) || (that.data.classbuild == '定静楼一'))
    // console.log(((that.data.classroomChange.indexOf(e.detail.value.classno.toUpperCase())) < 0))

    // console.log((that.data.classbuild == 0 && e.detail.value.classno.toUpperCase()))

    // console.log(((that.data.classbuild == 0) || (that.data.classbuild == '定静楼一')) && ((that.data.classroomChange.indexOf(e.detail.value.classno.toUpperCase())) < 0))
    e.detail.value.classno = e.detail.value.classno.trim()

    if (that.data.classbuild == undefined) {
      that.setData({
        check1: false
      })
    } else if (e.detail.value.classno == undefined || e.detail.value.classno == '') {
      that.setData({
        check2: false
      })
    } else if (((that.data.classbuild == 0) || (that.data.classbuild == '定静楼一')) && ((that.data.classroomChange.indexOf(e.detail.value.classno.toUpperCase())) < 0)) {
      that.setData({
        check6: false
      })

    } else if (((that.data.classbuild == 1) || (that.data.classbuild == '定静楼二')) && ((that.data.classroomChange2.indexOf(e.detail.value.classno.toUpperCase())) < 0)) {
      that.setData({
        check6: false
      })

    } else if (that.data.question == undefined) {
      that.setData({
        check3: false
      })
    } else if (this.data.question == 9) {
      if (e.detail.value.deta == undefined || e.detail.value.deta == '') {
        that.setData({
          check4: false
        })
      }

    }
    if (that.data.time_lim == undefined) {
      that.setData({
        check5: false
      })
    }

    // if (that.data.classbuild == 0) {
    //   for (let i = 0; i < that.data.classroomChange.length; i++) {
    //     if (that.data.classroomChange[i] == e.detail.value.classno.toUpperCase()) {
    //       that.setData({
    //         classno: i,
    //         deta: e.detail.value.deta
    //       })
    //     }
    //   }
    // }
    // if (that.data.classbuild == 1) {
    //   for (let i = 0; i < that.data.classroomChange2.length; i++) {
    //     if (that.data.classroomChange2[i] == e.detail.value.classno.toUpperCase()) {
    //       that.setData({
    //         classno: i + 77,
    //         deta: e.detail.value.deta
    //       })
    //     }
    //   }
    // }

    if (that.data.classbuild == 0) {
      that.data.classbuild = '定静楼一'
    } else if (that.data.classbuild == 1) {
      that.data.classbuild = '定静楼二'
    }


    switch (that.data.question) {
      case '0':
        that.data.question = '显示屏黑屏/异常/无法启动'
        break
      case '1':
        that.data.question = '主机无法启动'
        break
      case '2':
        that.data.question = '投影异常/模糊/倒置/无法投影'
        break
      case '3':
        that.data.question = '声音/音响问题'
        break
      case '4':
        that.data.question = '有线麦克风故障'
        break
      case '5':
        that.data.question = '无线麦克风故障'
        break
      case '6':
        that.data.question = '鼠标、键盘故障'
        break
      case '7':
        that.data.question = '网络连接异常'
        break
      case '8':
        that.data.question = 'U盘无法读取'
        break
      case '9':
        that.data.question = '其他'
        break
    }

    switch (that.data.time_lim) {
      case '0':
        that.data.time_lim = '马上处理'
        break
      case '1':
        that.data.time_lim = '课间处理'
        break
    }


    console.log('栋' + that.data.classbuild + '\n课室' + e.detail.value.classno.toUpperCase() + '\n问题' + that.data.question + '\n备注' + e.detail.value.deta + '\n时间' + that.data.time_lim)
    console.log(that.data.check1 + ' ' + that.data.check2 + ' ' + that.data.check3 + ' ' + that.data.check4 + ' ' + that.data.check5 + ' ' + that.data.check6)





    if (that.data.check1 && that.data.check2 && that.data.check3 && that.data.check4 && that.data.check5 && that.data.check6) {

      var date = new Date();
      var m = date.getMinutes().toString(); //获得当前分钟数




      console.log(date.getMinutes() + "获取时间")
      //开始缓存池中没有分钟数，当前分钟数肯定不等于缓存中的分钟数，当前可以执行
      if (m != wx.getStorageSync('m')) {
        console.log("发送请求")
        that.setData({
          loadModal: true
        })

        

        wx.login({
          success(res) {
            console.log(res.code);

            if (res.code) {
              //发起网络请求
              wx.request({
                url: 'https://classroom.light-year-tech.com:3001/api/req',
                method: 'post',
                data: {
                  //传入参数为code
                  code: res.code
                },
                success(res) {
                  console.log('调用后台接口成功')
                  console.log(res.data);
                  let openid = (JSON.parse(res.data)).openid
                  
                  wx.request({
                    // url: 'http://47.115.51.160:3000/api/profiles/add', //仅为示例，并非真实的接口地址
                    url: 'https://classroom.light-year-tech.com:3003/api/profiles/add', //仅为示例，并非真实的接口地址
                    method: "POST",
                    data: {
                      build: that.data.classbuild,
                      classroom: e.detail.value.classno.toUpperCase(),
                      type: that.data.question,
                      note: e.detail.value.deta,
                      limit: that.data.time_lim,
                      openid: openid
                    },

                    header: {
                      'content-type': 'application/json', // 默认值
                    },
                    success(res) {
                      console.log(res.data)
                      wx.setStorageSync('m', m) //把分钟数放到缓存
                      that.setData({
                        loadModal: false
                      })
                      that.showModal(true)

                      // 弹窗
                      // wx.requestSubscribeMessage({
                      //   tmplIds: ['TW8hmC2fYIWVV48pHkZ1vbbf8fQ4TSmBnjWHOfT1oT8'],
                      //   success(res) {
                      //     console.log('弹窗调用成功')
                      //   },
                      //   fail(res) {
                      //     console.log(res)
                      //   }
                      // })
                    },
                    fail(res) {
                      that.setData({
                        loadModal: false
                      })
                      that.showModal(2)
                    },

                  }) //要做的事情，提交啊，点击啊等等
                },
                fail(res) {
                  that.setData({
                    loadModal: false
                  })
                  that.showModal(2)
                  console.log('调用后台接口失败')
                }
              })
            } else {
              console.log('登录失败！' + res.errMsg)
            }
          }
        })


      } else {
        //当发生了1分钟内多次点击等事件，弹窗提示或者做别的操作。
        this.showModal(false)
        console.log("重复提交")
      }
    }

  },


  PickerChange: function(e) {
    console.log(e + "picker");
    this.setData({
      index: e.detail.value
    })
    var that = this
    that.setData({
      classbuild: e.detail.value
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  stringcut1: function(e) {
    return (e.substring(0, 1))
  },
  stringcut2: function(e) {
    return (e.substring(1, 5))
  },
  onLoad: function(options) {
    
    var scene = decodeURIComponent(options.scene)
    // 3736
    console.log(scene + 'scene')
    var classbuilddata = options.build;
    var classnodata = options.classroom;
    // if (scene != undefined && classbuilddata != '' && classnodata != '' && (typeof classbuilddata) == 'string') {
    //   classbuilddata = this.stringcut1(classbuilddata)
    //   classnodata = this.stringcut2(classnodata)
    // }

    console.log(classbuilddata)
    console.log(classnodata)

    this.setData({
      classbuilddata: classbuilddata,
      classnodata: classnodata,
      classbuild: classbuilddata,
      classno: classnodata
    })



    //   if (app.globalData.userInfo) {
    //     this.setData({
    //       userInfo: app.globalData.userInfo,
    //       hasUserInfo: true
    //     })
    //   } else if (this.data.canIUse) {
    //     // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //     // 所以此处加入 callback 以防止这种情况
    //     app.userInfoReadyCallback = res => {
    //       this.setData({
    //         userInfo: res.userInfo,
    //         hasUserInfo: true
    //       })
    //     }
    //   } else {
    //     // 在没有 open-type=getUserInfo 版本的兼容处理
    //     wx.getUserInfo({
    //       success: res => {
    //         app.globalData.userInfo = res.userInfo
    //         this.setData({
    //           userInfo: res.userInfo,
    //           hasUserInfo: true
    //         })
    //       }
    //     })
    //   }
    // },
    // getUserInfo: function(e) {
    //   console.log(e)
    //   app.globalData.userInfo = e.detail.userInfo
    //   this.setData({
    //     userInfo: e.detail.userInfo,
    //     hasUserInfo: true
    //   })
  },
  onShareAppMessage: function(res) {
    var that = this;
    return {
      title: '课室报障请点我！',
      path: '/pages/index/index',
      imageUrl: '../../images/bg.jpg',
      success: function(res) {
        // 转发成功

        that.shareClick();
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
  callPhone: function(e) {

    var phone = e.currentTarget.dataset.ph;



    wx.makePhoneCall({

      phoneNumber: phone

    })

  },

})